package niit.wei.service.impl;

import niit.wei.dao.CategoryDao;
import niit.wei.dao.impl.CategoryDaoImpl;
import niit.wei.entity.Category;
import niit.wei.service.CategoryService;
import niit.wei.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author WeiJinLong
 * @Date 2023-06-28 21:45
 * @Version 1.0
 */
public class CategoryServiceImpl implements CategoryService {
    private CategoryDao categoryDao = new CategoryDaoImpl();


    @Override
    public List<Category> findAll() throws SQLException, ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Connection conn = JDBCUtils.getConnection();
        String sql = "select category_id categoryId,category_name categoryName  from category";
        return categoryDao.getAllCategories(conn,sql,Category.class);
    }

    @Override
    public Category findById(Integer categoryId) throws SQLException, ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Connection conn = JDBCUtils.getConnection();
        String sql = "select category_id categoryId,category_name categoryName  from category where category_id =?";
        return categoryDao.getCategory(conn,sql,Category.class,categoryId);

    }
}
